package com.ruibang.glass.quality.component.job;

import com.ruibang.glass.quality.constant.Constants;
import com.ruibang.glass.quality.domain.req.DefectHourTaskReq;
import com.ruibang.glass.quality.domain.req.DefectLossReq;
import com.ruibang.glass.quality.domain.req.DiscardDefectReq;
import com.ruibang.glass.quality.service.TotalEquDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.CompletableFuture;

@Slf4j
@Component
public class DataDispose {

    @Autowired
    private TotalEquDataService totalEquDataService;

    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;


    /**
     * @Description: 小时任务
     * @version v1.0
     * @author songJinKang
     * @date 2024-04-03 10:46
     */
    public void hourTask(DefectHourTaskReq defectReq) {
        LocalDate endData = LocalDate.now();
        LocalDate startDate = LocalDate.now().minusDays(2);
        LocalDate discardStartData = LocalDate.now().minusDays(1);
        //热端
        CompletableFuture.runAsync(() -> {
            if (defectReq != null && (defectReq.getStartTime() != null && defectReq.getEndTime() != null)) {
                defectReq.setCheckMachineCode("CM2");
                totalEquDataService.importHotEndDiscardDefectData(defectReq);
            } else {
                discardDefectHotEndData(discardStartData, endData);
            }
            everyHourHotEndDiscardDefect(startDate, endData);
        },threadPoolTaskExecutor);
        //冷端
        CompletableFuture.runAsync(() -> {
            everyHourDefectLoss(startDate, endData);
            everyHourColdDirect(startDate, endData);
            discardDefectData(startDate, endData);
            everyHourDiscardDefect(discardStartData, endData);
        },threadPoolTaskExecutor);
    }

    /**
     * @Description: 热端任务
     * @version v1.0
     * @author songJinKang
     * @date 2024-03-25 20:10
     */
    public void hotTask(LocalDate startDate, LocalDate endData) {
        discardDefectHotEndData(startDate, endData);
        everydayHotEndDiscardDefect(startDate, endData);
        everyHourHotEndDiscardDefect(startDate, endData);
    }

    /**
     * @Description: 冷端任务
     * @version v1.0
     * @author songJinKang
     * @date 2024-03-25 20:10
     */
    public void coldTask(LocalDate startDate, LocalDate endData) {
        everyHourDefectLoss(startDate, endData);
        everydayDefectLoss(startDate, endData);
        everyHourColdDirect(startDate, endData);
        everydayColdDirect(startDate, endData);
        discardDefectData(startDate, endData);
        everydayDiscardDefect(startDate, endData);
        everyHourDiscardDefect(startDate, endData);
    }


    /**
     * @Description: 每小时缺陷损失数据
     * @version v1.0
     * @author songJinKang
     * @date 2023-09-08 10:34
     */
    public void everyHourDefectLoss(LocalDate start, LocalDate end) {
        String startDate = start.minusDays(3).format(DateTimeFormatter.ISO_LOCAL_DATE);
        String endDate = end.format(DateTimeFormatter.ISO_LOCAL_DATE);
        DefectLossReq defectLossReq = new DefectLossReq();
        defectLossReq.setStartDate(startDate);
        defectLossReq.setEndDate(endDate);
        totalEquDataService.importEveryHourDefectLoss(defectLossReq);
    }

    /**
     * @Description: 每日缺陷损失数据
     * @version v1.0
     * @author songJinKang
     * @date 2023-09-08 10:34
     */
    public void everydayDefectLoss(LocalDate start, LocalDate end) {
        String startDate = start.format(DateTimeFormatter.ISO_LOCAL_DATE);
        String endDate = end.format(DateTimeFormatter.ISO_LOCAL_DATE);
        DefectLossReq defectLossReq = new DefectLossReq();
        defectLossReq.setStartDate(startDate);
        defectLossReq.setEndDate(endDate);
        totalEquDataService.importEverydayDefectLoss(defectLossReq);
    }

    /**
     * @Description: 每小时冷端直投缺陷全检
     * @version v1.0
     * @author songJinKang
     * @date 2023-09-08 10:21
     */
    public void everyHourColdDirect(LocalDate start, LocalDate end) {
        int daysDiff = (int) ChronoUnit.DAYS.between(start, end);
        if (daysDiff > 0) {
            totalEquDataService.importEveryHourColdDirect(daysDiff);
        }
    }

    /**
     * @Description: 冷端直投缺陷全检每天数据
     * @version v1.0
     * @author songJinKang
     * @date 2023-09-08 10:29
     */
    public void everydayColdDirect(LocalDate start, LocalDate end) {
        int daysDiff = (int) ChronoUnit.DAYS.between(start, end);
        if (daysDiff > 0) {
            totalEquDataService.importEverydayColdDirect(Math.abs(daysDiff));
        }
    }


    /***
     * 抽取日常数据用于支撑判废数据查询(冷端)
     */
    public void discardDefectData(LocalDate start, LocalDate end) {
        String startDate = start.format(DateTimeFormatter.ISO_LOCAL_DATE);
        String endDate = end.format(DateTimeFormatter.ISO_LOCAL_DATE);
        DiscardDefectReq defectReq = new DiscardDefectReq();
        defectReq.setStartDate(startDate);
        defectReq.setEndDate(endDate);
        defectReq.setCheckMachineCode("CM1");
        //检查机类型0:冷端，1:热端
        defectReq.setCheckMachineType(Constants.ZERO_STR);
        //检查机类型0:半成品，1:成品
        defectReq.setIsFinished(Constants.ONE_STR);
        //调用
        totalEquDataService.importDiscardDefectData(defectReq);
    }

    /**
     * 每日判废缺陷数据（冷端）
     */
    public void everydayDiscardDefect(LocalDate start, LocalDate end) {
        String startDate = start.format(DateTimeFormatter.ISO_LOCAL_DATE);
        String endDate = end.format(DateTimeFormatter.ISO_LOCAL_DATE);
        DiscardDefectReq defectReq = new DiscardDefectReq();
        defectReq.setStartDate(startDate);
        defectReq.setEndDate(endDate);
        defectReq.setCheckMachineCode("CM1");
        //检查机类型0:冷端，1:热端
        defectReq.setCheckMachineType(Constants.ZERO_STR);
        //检查机类型0:半成品，1:成品
        defectReq.setIsFinished(Constants.ONE_STR);
        //调用
        totalEquDataService.importEverydayDiscardDefect(defectReq);
    }


    /**
     * 每小时判废缺陷数据（冷端）
     */
    public void everyHourDiscardDefect(LocalDate start, LocalDate end) {
        String startDate = start.format(DateTimeFormatter.ISO_LOCAL_DATE);
        String endDate = end.format(DateTimeFormatter.ISO_LOCAL_DATE);
        DiscardDefectReq defectReq = new DiscardDefectReq();
        defectReq.setStartDate(startDate);
        defectReq.setEndDate(endDate);
        defectReq.setCheckMachineCode("CM1");
        //检查机类型0:冷端，1:热端
        defectReq.setCheckMachineType(Constants.ZERO_STR);
        //检查机类型0:半成品，1:成品
        defectReq.setIsFinished(Constants.ONE_STR);
        //调用
        totalEquDataService.importEveryHourDiscardDefect(defectReq);
    }


    /***
     * 抽取日常数据用于支撑判废数据查询(热端)--------------------------------------热端-------------------------------------------------
     */
    public void discardDefectHotEndData(LocalDate start, LocalDate end) {
        String startDate = start.format(DateTimeFormatter.ISO_LOCAL_DATE);
        String endDate = end.format(DateTimeFormatter.ISO_LOCAL_DATE);
        DiscardDefectReq defectReq = new DiscardDefectReq();
        defectReq.setStartDate(startDate);
        defectReq.setEndDate(endDate);
        defectReq.setCheckMachineCode("CM2");
        //检查机类型0:冷端，1:热端
        defectReq.setCheckMachineType(Constants.ONE_STR);
        //检查机类型0:半成品，1:成品
        defectReq.setIsFinished(Constants.ZERO_STR);
        //调用
        totalEquDataService.importDiscardDefectData(defectReq);
    }


    /**
     * 每日判废缺陷数据（热端）
     */
    public void everydayHotEndDiscardDefect(LocalDate start, LocalDate end) {
        String startDate = start.format(DateTimeFormatter.ISO_LOCAL_DATE);
        String endDate = end.format(DateTimeFormatter.ISO_LOCAL_DATE);
        DiscardDefectReq defectReq = new DiscardDefectReq();
        defectReq.setStartDate(startDate);
        defectReq.setEndDate(endDate);
        defectReq.setCheckMachineCode("CM2");
        //检查机类型0:冷端，1:热端
        defectReq.setCheckMachineType(Constants.ONE_STR);
        //检查机类型0:半成品，1:成品
        defectReq.setIsFinished(Constants.ZERO_STR);
        //调用
        totalEquDataService.importEverydayDiscardDefect(defectReq);
    }

    /**
     * 每小时判废缺陷数据(热端)
     */
    public void everyHourHotEndDiscardDefect(LocalDate start, LocalDate end) {
        String startDate = start.format(DateTimeFormatter.ISO_LOCAL_DATE);
        String endDate = end.format(DateTimeFormatter.ISO_LOCAL_DATE);
        DiscardDefectReq defectReq = new DiscardDefectReq();
        defectReq.setStartDate(startDate);
        defectReq.setEndDate(endDate);
        defectReq.setCheckMachineCode("CM2");
        //检查机类型0:冷端，1:热端
        defectReq.setCheckMachineType(Constants.ONE_STR);
        //检查机类型0:半成品，1:成品
        defectReq.setIsFinished(Constants.ZERO_STR);
        //调用
        totalEquDataService.importEveryHourDiscardDefect(defectReq);
    }


}
